package chrisbode.cps142.a2;

public abstract class MovieTicket {
	private final String title;

	private final int totalSeats;
	private int soldSeats;

	public MovieTicket(String title, int totalSeats, int soldSeats) {
		this.title = title;

		this.totalSeats = totalSeats;
		this.soldSeats = soldSeats;
	}

	/**
	 * @return the soldSeats
	 */
	public int getSoldSeats() {
		return soldSeats;
	}

	/**
	 * @param soldSeats
	 *            the soldSeats to set
	 */
	public void setSoldSeats(int soldSeats) {
		this.soldSeats = soldSeats;
	}

	/**
	 * Increments the count of sold seats.
	 * @throws TheatreCapacityException 
	 */
	public void incrementSoldSeats() throws TheatreCapacityException {
		if (this.getSoldSeats() < this.getTotalSeats()) {
			this.soldSeats++;
		} else {
			throw new TheatreCapacityException();
		}
	}

	/**
	 * Decrements the count of sold seats.
	 */
	public void decrementSoldSeats() {
		if (this.getSoldSeats() > 0)
			this.soldSeats--;
	}

	/**
	 * 
	 * @return true if seats are available.
	 */
	public boolean areSeatsAvailable() {
		return this.getSoldSeats() < this.getTotalSeats();
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @return the totalSeats
	 */
	public int getTotalSeats() {
		return totalSeats;
	}

	public abstract double getPrice(Patron patronType);

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "MovieTicket [title=" + title + ", totalSeats=" + totalSeats
				+ ", soldSeats=" + soldSeats + "]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + soldSeats;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result + totalSeats;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof MovieTicket))
			return false;

		// The following condition makes equals methods in the three subclasses
		// of MovieTicket redundant.

		if (!(obj.getClass().equals(this.getClass())))
			return false;

		MovieTicket other = (MovieTicket) obj;
		if (soldSeats != other.soldSeats)
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (totalSeats != other.totalSeats)
			return false;
		return true;
	}
}
